package com.easthome.zsp;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Demo03 {
    public static void main(String[] args) {
        try {
            Class aclass = Class.forName("com.easthome.zsp.PrivateTest");
            PrivateTest o = (PrivateTest)aclass.newInstance();
            Field name = aclass.getDeclaredField("name");
            name.setAccessible(true);
            name.set(o,"无敌起飞");
            System.out.println(o.getName());
            Method getName = aclass.getMethod("getName");
            Object invoke = getName.invoke(o);
            System.out.println("name的属性值为："+invoke);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //反射获取file类的所有构造器
        try {
            Class<?> aClass = Class.forName("java.io.File");
            Constructor<?>[] declaredConstructors = aClass.getDeclaredConstructors();
            System.out.println(declaredConstructors.length+"长度噢");
           for (int i = 0; i <declaredConstructors.length ; i++) {
                System.out.println("第"+i+"个构造器：：："+declaredConstructors[i]);
            }
            Constructor<?> constructor = aClass.getDeclaredConstructor(String.class);
            constructor.setAccessible(true);
            String path = "d:\\aaaaaaa.txt";
            File o = (File)constructor.newInstance(path);
            System.out.println(o.createNewFile());
            System.out.println("文件创建成功+"+path);

        } catch (Exception e) {
            e.printStackTrace();
        }


    }


}

